/**
 * 
 */
package net.mikoo.seals.manage.web.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import net.mikoo.seals.biz.service.CustosService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.model.Custos;
import net.mikoo.seals.model.CustosQuery;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.UserLogin;

/**
 * @author huangxm
 *
 */
public class VerifyInterceptor implements HandlerInterceptor {

	Logger logger = Logger.getLogger(VerifyInterceptor.class);
	
	
	@Autowired
	private CustosService custosService;
	

	/**
	 * 
	 */
	public VerifyInterceptor() {
	}
	
	
	

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		

		HttpSession session = request.getSession();
		Object obj = session.getAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		Party party = (Party)obj;
		UserLogin userLogin = party.getUserLogin();
		
		//判断是否是系统管理员
		if(userLogin.getIsSystem().equals("Y")) {
			
			session.setAttribute(WebContext.ADMIN_PERMISSION_SESSION_KEY, "ADMIN");
			
			return true;
		}
		
		
		//判断是否是印章管理员
		CustosQuery query = new CustosQuery();
		query.setCompanyId(party.getCompanyId());
		query.setPartyId(party.getPartyId());
		List<Custos> custoses = custosService.getListByQuery(query);
		if(custoses != null && !custoses.isEmpty()) {
			
			session.setAttribute(WebContext.ADMIN_PERMISSION_SESSION_KEY, "CUSTOS");
			
			return true;
		}
		
		
		session.setAttribute(WebContext.ADMIN_PERMISSION_SESSION_KEY, "STAFF");
		
		//其他
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub

	}

}
